package com.google.code.fulltank;

/**
 * A finite fuel tank.
 * 
 * @author DL
 */
public class FuelTank {
    /**
     * Constructor.
     * @param size         Fuel tank's size (in litres).
     * @param fuelAmount   Initial fuel amount (in litres).
     */
    public FuelTank(double size, double fuelAmount) {
        this.size = size;
        this.fuelAmount = fuelAmount;
    }

    /** @return the fuel tank's size (in litres). */
    public double getSize() {
        return size;
    }

    /** @return the fuel tank's current fuel amount size (in litres). */
    public double getFuelAmount() {
        return fuelAmount;
    }

    /**
     * Fills up the fuel tank.
     * @param amount   Fuel amount (in litres) to fill the tank with.
     *                 Double.POSITIVE_INFINITY to fill up the tank completely.
     * @return the actual fuel amount filled.
     */
    public double fill(double amount) {
        if (amount <= 0) {
            throw new IllegalArgumentException("Fuel amount must be positive");
        }

        double amountFilled = Math.min(size - fuelAmount, amount);
        fuelAmount += amountFilled;
        
        if (amountFilled > size) {
            System.out.println("****");
        }
        return amountFilled;
    }

    /**
     * Burns (uses) fuel in the tank.
     * @param amount   Fuel amount (in litres) to burn.
     * @return the actual fuel amount filled.
     */
    public void burn(double amount) {
        if (amount > fuelAmount) {
            System.out.println(amount + " --- " + fuelAmount);
            throw new IllegalArgumentException("Not enough fuel to burn");
        }

        fuelAmount -= amount;
    }

    private final double size;
    private double fuelAmount;
}
